package com.bestcem.xm.award.grpc.client;

import com.bestcem.xm.award.service.dto.DeliverRpcDTO;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import zhongyan.xm.base.v1.Base;

/**
 * 投放 grpc客户端
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/23 11:35
 */
@Slf4j
@Service
public class DeliverGrpcService {

    @GrpcClient(GrpcClientNames.DS_DELIVER)
    private BpDeliverServiceGrpc.BpDeliverServiceBlockingStub stub;

    /**
     * 查看奖励是否被投放使用中
     *
     * @param request
     * @return
     */
    public CheckAwardUsedResponse checkAwardUsed(CheckAwardUsedRequest request) {
        try {
            if (log.isInfoEnabled()) {
                log.info("查看奖励是否被投放使用中请求参数{}", request.getId());
            }
            CheckAwardUsedResponse checkAwardUsedResponse = stub.checkAwardUsed(request);
            if (log.isInfoEnabled()) {
                log.info("查看奖励是否被投放使用中{},{}", checkAwardUsedResponse.getStatus().getCodeValue(), checkAwardUsedResponse.getUse());
            }
            return checkAwardUsedResponse;
        } catch (Exception e) {
            log.error("[award] deliver checkAwardUsed {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return CheckAwardUsedResponse.newBuilder().setStatus(error("deliver服务调用失败")).build();
        }
    }

    /**
     * Info
     * 获取单个投放
     *
     * @param request
     * @return
     */
    public GetDeliverInfoResponse getDeliver(GetDeliverInfoRequest request) {
        try {
            return stub.getDeliverInfo(request);
        } catch (Exception e) {
            log.error("[award] deliver getDeliver {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return GetDeliverInfoResponse.newBuilder().setStatus(error("deliver服务调用失败")).build();
        }
    }

    /**
     * 获取单个投放
     *
     * @param deliverId
     * @return
     */
    public DeliverRpcDTO getDeliverDto(String deliverId) {
        if (StringUtils.isBlank(deliverId)) {
            return null;
        }
        GetDeliverInfoRequest.Builder builder = GetDeliverInfoRequest.newBuilder();
        builder.setId(deliverId);
        GetDeliverInfoResponse deliverResponse = this.getDeliver(builder.build());
        if (deliverResponse.getStatus().getCodeValue() != com.bestcem.bp.grpc.v1.base.Code.OK_VALUE) {
            log.error("查询getDeliver失败 {},{}", deliverResponse.getStatus().getCode(), deliverResponse.getStatus().getMessage());
            return null;
        } else {
            DeliverRpcDTO deliver = new DeliverRpcDTO();
            deliver.setChannel(deliverResponse.getDeliver().getSourceValue());
            deliver.setName(deliverResponse.getDeliver().getName());
            return deliver;
        }
    }


    /**
     * 根据奖励id结束投放
     *
     * @param request
     * @return
     */
    //public StopDeliverResponse stopDeliver(StopDeliverRequest request) {
    //    try {
    //        return stub.stopDeliver(request);
    //    } catch (Exception e) {
    //        log.error("[award] deliver stopDeliver {} Error; ErrorMsg: {};", request, e.getMessage(), e);
    //        return StopDeliverResponse.newBuilder().setStatus(error("deliver服务调用失败")).build();
    //    }
    //}


    /**
     * 构造错误状态
     *
     * @param message 错误信息
     * @return
     */
    private Base.Status error(String message) {
        Base.Status.Builder status = Base.Status.newBuilder();
        status.setMessage(message);
        status.setCode(Base.Code.ERROR);
        return status.build();
    }
}
